﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Dao;
using LinkCard.Model;
using LinkCard.Utils;
using System.Data;


namespace LinkCard.ShopCP
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            error.Visible = false;
        }

        protected void checkLogin(string sUsername, string sUserPw, string loginas)
        {
            //var supplierID = Convert.ToInt32(sUsername);
            
            TopLinkEntities topLinkDb = new TopLinkEntities();
            var supplierDb = new Supplier();
            var shopStaff = new ShopStaff();

            if (loginas == "0") //Nhân viên bán hàng
            {
                shopStaff = topLinkDb.ShopStaffs.Where(t => t.Username == sUsername && t.Password == sUserPw).FirstOrDefault();
                if (shopStaff != null)
                {
                    if (shopStaff.Status == -1)
                    {
                        error.Visible = true;
                        error.InnerHtml = "Tài khoản hiện đang bị khóa";
                    }
                    else
                    {
                        Session["USER"] = sUsername;
                        Session["ShopStaffID"] = shopStaff.UserID;
                        Session["SHOPADMIN"] = 0;
                        Session.Timeout = 19;

                        //Lưu log
                        var logAction = LogActions.LogItem(0, getSupplierID(), 0, 0, shopStaff.UserID, "Đăng nhập", "Login ShopStaff", "ShopCP");

                        Response.Redirect("Sell/Sell.aspx");
                    }
                }
                else
                {
                    error.Visible = true;
                }
            }
            else if (loginas == "1") //Quản lý cửa hàng
            {
                //var supplierID = Convert.ToInt32(sUsername);
                supplierDb = topLinkDb.Supplier.Where(t => t.Username == sUsername && t.Password == sUserPw).FirstOrDefault();
                if (supplierDb != null)
                {
                    if (supplierDb.Status == -1)
                    {
                        error.Visible = true;
                        error.InnerHtml = "Tài khoản hiện đang bị khóa";
                    }
                    else
                    {
                        Session["USER"] = supplierDb.SupplierID;
                        Session["ShopStaffID"] = "";
                        Session["SHOPADMIN"] = 1;
                        Session.Timeout = 19;
                        //Lưu log
                        var logAction = LogActions.LogItem(0, supplierDb.SupplierID, 0, 0, 0, "Đăng nhập", "Login ShopCP", "ShopCP");
                        Response.Redirect("dashboard.aspx");
                    }
                }
                else
                {
                    error.Visible = true;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string loginAs = loginas.SelectedIndex.ToString();
            string sUsername = Username.Value.ToUpper().Trim();
            sUsername = LinkCard.Utils.hash1way.SQLPatch(sUsername);
            string sUserPw = Password.Value.ToUpper().Trim(); 
            sUserPw = LinkCard.Utils.hash1way.SQLPatch(sUserPw);
            checkLogin(sUsername, sUserPw, loginAs);
        }
        protected Int64 getSupplierID()
        {
            try
            {

                if (Convert.ToInt32(Session["SHOPADMIN"]) == 1) // quanr ly cua hang
                {
                    Session["SupplierID"] = ConvertUtility.ToInt64(Session["USER"]);
                    return ConvertUtility.ToInt64(Session["USER"]);
                }
                else if (Convert.ToInt32(Session["SHOPADMIN"]) == 0)
                {
                    DataTable dt = LinkCard.AdminCP.Common.DataHelper.SelectTable("SELECT SupplierID FROM [ShopStaff] WHERE Username ='" + Session["USER"].ToString() + "'", false);
                    if (dt.Rows.Count > 0)
                    {
                        Session["SupplierID"] = ConvertUtility.ToInt64(dt.Rows[0]["SupplierID"]);
                        return ConvertUtility.ToInt64(dt.Rows[0]["SupplierID"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}